package com.dtsz.m2.ui.base;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.android.volley.VolleyError;
import com.dtsz.m2.provider.DataUtils;
import com.dtsz.m2.ui.model.PageVo;
import com.dtsz.m2.ui.view.LoadingDailog;
import com.dtsz.m2.volley.httpUtil.HCUParame;
import com.dtsz.m2.volley.httpUtil.HUCRequest;
import com.dtsz.m2.volley.httpUtil.interfase.HUCCallBackInterfase;
import com.dtsz.m2.volley.httpUtil.interfase.HUCParameInterfase;
import com.dtsz.m2.volley.httpUtil.interfase.HUCRequestInterfase;


public class BaseFragment extends Fragment implements View.OnClickListener,
        HUCCallBackInterfase {
    // 网络请求接口类
    public HUCRequestInterfase hucRequestInterfase;
    // 网络请求构参接口类
    public HUCParameInterfase hucParameInterfase;

    public PageVo pageVo = new PageVo();


    //声明SharedPreferences，用来读取xml;
    private SharedPreferences sp;
    //声明SharedPreferences.Editor，用来修改xml里面的值。
    private SharedPreferences.Editor ed;
    private LoadingDailog dialog1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initUtils();
        initView();
        initClick();
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * 初始化当前视图
     */
    public void initView() {

    }

    /**
     * 绑定监听
     */
    public void initClick() {

    }

    /**
     * 初始化工具类
     */
    public void initUtils() {
        sp = getActivity().getSharedPreferences("setting", getActivity().MODE_PRIVATE);
        String token = sp.getString("tokenRSA", "token001");
        pageVo.setToken(token);
        DataUtils.setMyPayToken(getActivity(), token);
        hucRequestInterfase = HUCRequest.getInstance();
        hucParameInterfase = HCUParame.getInstance();
    }

    public void setSharedPreferences(String key, String vlaue) {
        ed = sp.edit();
        ed.putString(key, vlaue);//保存token
        ed.commit();
    }

    public String getSharedPreferences(String key) {
        sp = getActivity().getSharedPreferences("setting", getActivity().MODE_PRIVATE);
        String token = sp.getString(key, "token001");
        return token;
    }

    /**
     * 点击事件
     */
    public void onClick(View v) {
    }

    public void onSuccessfulBitampType(int inputType, Bitmap response) {
        // TODO Auto-generated method stub

    }

    public void onErrorResponse(int inputType, VolleyError response) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onErrorLogin(int inputType) {

    }

    public void onSuccessfulDB(int inputType, Object obj) {
        // TODO Auto-generated method stub
    }

    public void onSuccessfulStrType(int inputType, Object response) {
        // TODO Auto-generated method stub

    }

    public void sendIntent(Activity activity, Class myClass) {
        Intent intent = new Intent();
        intent.setClass(activity, myClass);
        startActivity(intent);
    }

    public void loading() {
        LoadingDailog.Builder builder1 = new LoadingDailog.Builder(getActivity())
                .setMessage("加载中...")
                .setCancelOutside(true)
                .setCancelable(true);
        if (dialog1 == null) {
            dialog1 = builder1.create();
            dialog1.show();
        }
        dialog1.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (dialog1 != null) {
                    hucRequestInterfase.cancelAll();
                    dialog1.dismiss();
                }
                dialog1 = null;
            }
        });
    }

    public void loadDismiss() {
        if (dialog1 != null) {
            dialog1.dismiss();
        }
        dialog1 = null;
    }

    public void sendUrl(String url) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        startActivity(intent);
    }

    public void showToast(String toastContent) {
        Toast.makeText(getActivity(), toastContent, Toast.LENGTH_SHORT).show();
    }
}
